<?php
class ErrorController extends Zend_Controller_Action
{
    private $_exception;
    private static $_errorMessage;
    private static $_httpCode;
    
    protected static $_productionEnvironments = array('production'); 
    
    public function init()
    {
               
    }
    
    public function preDispatch()
    {
    	$this->_exception = $this->_getParam('error_handler');
    	
    	switch ($this->_exception->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
                self::$_httpCode = 404;
                self::$_errorMessage = 'Page introuvable';
            break;
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER:
            	switch (get_class($this->_exception->exception)) {
            		case 'Zend_View_Exception' :
            			self::$_httpCode = 500;
            			self::$_errorMessage = 'Erreur de traitement d\'une vue: '.$this->_exception->exception->getMessage();
            		break;
            		default:
            			self::$_httpCode = 500;
            			self::$_errorMessage = $this->_exception->exception->getMessage();
            		break;
            	}
            break;
    	}
        
        // si erreur 404 ...
        if (404 == self::$_httpCode) {
            $request = Zend_Registry::get('originalRequest');
            setvar('is404', true);
            $this->_forward('is404', 'static', $request->getModuleName());
        }
    	
    	if (500 == self::$_httpCode) {
    	    $request = Zend_Registry::get('originalRequest');
            setvar('is505', true);
            setvar('errorDisplay', self::$_errorMessage);
            $this->_forward('is500', 'static', $request->getModuleName());
        }
    }
    
    public function errorAction()
    {
        if(($logh = $this->getInvokeArg('bootstrap')->getResource('log')) instanceof Zend_Log) {
            $logh->log("trapped error on application: " . $this->_exception->exception->getMessage(), Zend_Log::ERR);
        }        
        
        $formattedTrace = $this->_exception->exception->getTraceAsString();
        
    	$this->getResponse()->setHttpResponseCode(self::$_httpCode);
    	$this->_errorMessage .= sprintf("<p>%s</p> with back trace:<pre>%s</pre>", print_r(self::$_errorMessage, true), $formattedTrace);
    	$this->view->errorMessage = $this->_errorMessage;
    	$this->view->httpCode     = self::$_httpCode;
    	$this->view->productionEnvironments = self::$_productionEnvironments;
    	
    	$this->view->headTitle('La page que vous demandez n\'est pas disponible - AJF Finance');
        
        //trouver un autre moyen de passer le bodyId au layout
        //$this->getResponse()->append("bodyId", "error404");
    }
    
    public function postDispatch()
    {
    }
}
